package redis;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.params.SetParams;

import java.util.Set;

public class RedisDemoGenral {
    public static  void  test4(Jedis jedis) throws InterruptedException {
        System.out.println("expire 和 ttl");
        jedis.flushAll();

        jedis.set("key1","123");
        jedis.set("key3","789");
        jedis.set("key2","456");

        jedis.expire("key1",10);

        long t  = jedis.ttl("key1");
        System.out.println(t);
        Thread.sleep(3000);
        t  = jedis.ttl("key1");
        System.out.println(t);
        Thread.sleep(3000);

        t  = jedis.ttl("key1");
        System.out.println(t);
        Thread.sleep(3000);

        t  = jedis.ttl("key1");
        System.out.println(t);
    }
    public static void test3(Jedis jedis){
        System.out.println("exists 和 del");
        jedis.flushAll();

        jedis.set("key1","123");
        jedis.set("key4","456");
        jedis.set("key3","789");
        jedis.set("key2","456");


        Set<String> keys = jedis.keys("*");
        System.out.println(keys);

    }
    public static void  test2(Jedis jedis){
        System.out.println("exists 和 del");
        jedis.flushAll();

        jedis.set("key1","123");
        jedis.set("key2","456");
        jedis.set("key3","789");

        boolean rest = jedis.exists("key1");
        System.out.println("key1 is exists:"+rest);

        long rest2 = jedis.del("key1");
        System.out.println("del rest2:"+rest2);

         rest = jedis.exists("key1");
        System.out.println("key1 is exists:"+rest);

        rest2 = jedis.del("key2","key3");
        System.out.println("some del :"+rest2);
    }
    public static void test1(Jedis jedis) {
        System.out.println("get 和 set 的使用");
        jedis.flushAll();//清空数据库

        jedis.set("key1","123");
        jedis.set("key2","456");

        SetParams params = new SetParams();
        params.ex(10);//ex设置过期时间
        params.xx();//xx key存在的情况
        //params.nx();//xx key不存在的情况

        jedis.set("key1","777",params);

        String value = jedis.get("key1");
        System.out.println("key1="+value);


    }

    public static void test5(Jedis jedis) {
        System.out.println("type");
        jedis.flushAll();//清空数据库

        jedis.set("key","123");
        String t = jedis.type("key");
        System.out.println("string type:" + t);

        jedis.lpush("key2","111","2222");
        t = jedis.type("key2");
        System.out.println("list type:" + t);


        jedis.hset("key3","1","12");
        t = jedis.type("key3");
        System.out.println("hash key3:"+t);

        jedis.sadd("key4","123","124","125");
        t = jedis.type("key4");
        System.out.println("set key4:"+t);

        jedis.zadd("key5",10,"123");
        t = jedis.type("key5");
        System.out.println("zset key5:"+t);

    }
    public static void main(String[] args) {
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");

        try(Jedis jedis = jedisPool.getResource()) {
            /*String song = jedis.ping();
            System.out.println(song);*/
            //test1(jedis);
            //test2(jedis);
            //test3(jedis);
            //test4(jedis);
            test5(jedis);

        }
    }
}
